// $codepro.audit.disable numericLiterals
/**
 * lab order panel
 */
package edu.gatech.oad.doctorsorder;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JButton;

import edu.gatech.oad.ui.UserPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.logging.Logger;

/**
 * panel to display lab order
 * 
 * @author chen
 * @version 1.0
 */
public class LabOrderPanel extends JPanel {
	/**
	 * initialize variables
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * order's name
	 */
	private final JTextField nameField;

	/**
	 * order's location
	 */
	private final JTextField locField;

	/**
	 * order's cost
	 */
	private final JTextField costField;

	/**
	 * the user type that is allowed to edit the content;s
	 */
	private static final String[] PRIVILEGEUSRTYPE = {
			"edu.gatech.oad.user.Nurse", "edu.gatech.oad.user.Doctor" };

	/**
	 * Create the panel.
	 * 
	 * @param labOrder
	 *            lab order to view/edit
	 * 
	 * @param userPanel
	 *            the panel to display in
	 */
	public LabOrderPanel(final LabOrder labOrder, UserPanel userPanel) {
		final GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 450, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 200, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		final JPanel name = new JPanel();
		final GridBagConstraints gbc_name = new GridBagConstraints();
		gbc_name.insets = new Insets(0, 0, 5, 0);
		gbc_name.fill = GridBagConstraints.HORIZONTAL;
		gbc_name.gridx = 0;
		gbc_name.gridy = 0;
		add(name, gbc_name);
		name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));

		final JLabel lblName = new JLabel("Name: ");
		name.add(lblName);

		nameField = new JTextField();
		nameField.setText(labOrder.getName());
		name.add(nameField);

		nameField.setColumns(30);

		final JPanel loc = new JPanel();
		final GridBagConstraints gbc_loc = new GridBagConstraints();
		gbc_loc.insets = new Insets(0, 0, 5, 0);
		gbc_loc.fill = GridBagConstraints.HORIZONTAL;
		gbc_loc.gridx = 0;
		gbc_loc.gridy = 1;
		add(loc, gbc_loc);
		loc.setLayout(new BoxLayout(loc, BoxLayout.X_AXIS));

		final JLabel lblLocation = new JLabel("Location: ");
		lblLocation.setHorizontalAlignment(SwingConstants.LEFT);
		loc.add(lblLocation);

		locField = new JTextField();
		loc.add(locField);
		locField.setColumns(28);
		locField.setText(labOrder.getLocation());

		final JPanel note = new JPanel();
		final GridBagConstraints gbc_note = new GridBagConstraints();
		gbc_note.insets = new Insets(0, 0, 5, 0);
		gbc_note.fill = GridBagConstraints.BOTH;
		gbc_note.gridx = 0;
		gbc_note.gridy = 2;
		add(note, gbc_note);
		note.setLayout(new BoxLayout(note, BoxLayout.X_AXIS));

		final JLabel lblNote = new JLabel("Note:");
		note.add(lblNote);

		final JScrollPane scrollPane = new JScrollPane();
		note.add(scrollPane);

		final JEditorPane noteField = new JEditorPane();

		noteField.setText(labOrder.getNotes());
		scrollPane.setViewportView(noteField);

		final JPanel costPanel = new JPanel();
		final GridBagConstraints gbc_costPanel = new GridBagConstraints();
		gbc_costPanel.insets = new Insets(0, 0, 5, 0);
		gbc_costPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_costPanel.gridx = 0;
		gbc_costPanel.gridy = 3;
		add(costPanel, gbc_costPanel);
		costPanel.setLayout(new BoxLayout(costPanel, BoxLayout.X_AXIS));

		final JLabel cost = new JLabel("Cost: ");
		costPanel.add(cost);

		costField = new JTextField();
		costPanel.add(costField);
		costField.setColumns(30);
		costField.setText(Double.toString(labOrder.getCost()));

		final JPanel btnPanel = new JPanel();
		final GridBagConstraints gbc_btnPanel = new GridBagConstraints();
		gbc_btnPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnPanel.gridx = 0;
		gbc_btnPanel.gridy = 4;
		add(btnPanel, gbc_btnPanel);

		final JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				labOrder.setName(nameField.getText());
				labOrder.setLocation(locField.getText());
				labOrder.setNotes(noteField.getText());
				try {
					labOrder.setCost(Double.valueOf(costField.getText()));
				} catch (Exception e) {
					Logger.getAnonymousLogger().warning("number format");
				}

			}
		});
		btnPanel.add(btnSave);

		// check edit privilege
		nameField.setEditable(false);
		locField.setEditable(false);
		noteField.setEditable(false);
		costField.setEditable(false);
		for (String tmp : LabOrderPanel.PRIVILEGEUSRTYPE) {
			String className = userPanel.getUser().getClass().getName();
			if (className.equals(tmp)) {
				nameField.setEditable(true);
				locField.setEditable(true);
				noteField.setEditable(true);
				costField.setEditable(true);
			}
		}
	}

}
